package com.phenix.share.demo

import android.util.Log
import androidx.lifecycle.ViewModel
import com.phenix.share.superwindow.ISuperFloatingWindowCallback
import com.phenix.share.superwindow.SuperFloatingWindowManager
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow

class MainViewModel : ViewModel() {


    private val callback = object : ISuperFloatingWindowCallback {
        override fun onWindowPositionChanged(x: Int, y: Int) {
            Log.d(TAG, "#onWindowPositionChanged x:$x y:$y")
        }
    }

    // 使用 StateFlow 暴露只读状态
    private val _autoAttachToEdges = MutableStateFlow(true)
    val autoAttachToEdges: StateFlow<Boolean> = _autoAttachToEdges.asStateFlow()

    // 切换状态的方法
    fun toggleAutoAttachToEdges() {
        _autoAttachToEdges.value = !_autoAttachToEdges.value
    }

    private val _showOnConnected = MutableStateFlow(true)
    val showOnConnected: StateFlow<Boolean> = _showOnConnected.asStateFlow()

    fun toggleShowOnConnected() {
        _showOnConnected.value = !_showOnConnected.value
    }

    fun setShowOnConnected(value: Boolean) {
        _showOnConnected.value = value
    }

    private val _draggable = MutableStateFlow(true)
    val draggable: StateFlow<Boolean> = _draggable.asStateFlow()

    fun toggleDraggable() {
        _draggable.value = !_draggable.value
    }

    fun setDraggable(value: Boolean) {
        _draggable.value = value
    }


    init {
        Log.d(TAG, "#init")
        SuperFloatingWindowManager.getInstance().addOnSuperFloatingWindowCallback(callback)
    }

    override fun onCleared() {
        super.onCleared()
        Log.d(TAG, "#onCleared")
    }

    companion object {
        private const val TAG = "MainViewModel"
    }
}